package com.bjpowernode.controller;


import com.bjpowernode.domain.Product;
import com.bjpowernode.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Arrays;
import java.util.List;

@Controller
public class ProductController {
    @Resource
    private ProductService productService;

    @RequestMapping("/listProduct.do")
    @ResponseBody
    public List<Product> getListProduct(){
        System.out.println("触发了");

        List<Product> productList = productService.ListProduct();

        return productList;
    }

    @RequestMapping("/addProduct.do")
    @ResponseBody
    public int addProduct(Product product, MultipartFile upFile, HttpServletRequest request) throws IOException {
        //获取文件名：
        String filename = upFile.getOriginalFilename();
        //指定文件完整路径：也就是将上传的文件放入到指定的目录下
        filename ="d:/products/"+filename;
        //上传文件
        upFile.transferTo(new File(filename));
        //将对象属性作相应修改
        product.setImgurl(filename);
        //调用逻辑层
        int i = productService.addProduct(product);
        return i;
    }

    @RequestMapping("/showImg.do")
    @ResponseBody
    public void getImgurl(String url, HttpServletResponse response) throws Exception {
        //通过前端获取到文件的路径url:前端传来的url就是文件的路径：d:/products/c_0028.jpg
        //利用IO流根据路径读取该文件：
        System.out.println(url);
        FileInputStream in = new FileInputStream(url);
        //通过servlet获取输出流：
        ServletOutputStream out = response.getOutputStream();
        //边读边写：
        byte[] bytes =new byte[1024];
        int len;
        while ((len=in.read(bytes)) !=-1){
            out.write(bytes,0,len);
        }
        out.close();
        in.close();
    }

    //单例删除：因为页面是通过ajax实现的动态页面，所以单例删除要借助父类标签实现删除功能
    @RequestMapping("/deleteProduct.do")
    @ResponseBody
    public int deleteProduct(String id){
        System.out.println(id);
        int i = productService.deleteProduct(Integer.parseInt(id));
        return i;
    }

    @RequestMapping("/deleteMoreProducts")
    @ResponseBody
    public int deleteMoreProducts(String[] ids){
        int i = productService.deleteMoreProducts(ids);
        return i;

    }

    @RequestMapping("/editSelectProduct.do")
    public ModelAndView editProduct(String id){
        int eid = Integer.parseInt(id);
        ModelAndView mv = new ModelAndView();
        Product product = productService.editSelectProduct(eid);
        mv.addObject("product",product);
        System.out.println(product.getImgurl());
        mv.setViewName("product_update");
        return mv;
    }

    @RequestMapping("/editProductById.do")
    public int editProduct(Product product){
        System.out.println(product);
        return 0;
    }
    @RequestMapping("/aa")
    public void selectWithType(){
        System.out.println("aa");
        Product product = productService.selectWithType(20);
        System.out.println(product);
    }
}
